#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

DEB_DESTDIR = debian/rabbitmq-server
RABBITMQ_USER ?= rabbitmq
RABBITMQ_GROUP ?= rabbitmq

unexport DEPS_DIR

%:
	dh $@ --parallel --with systemd

override_dh_auto_clean:
	$(MAKE) clean clean-unpacked-source-dist distclean-manpages
	rm -rf .erlang.mk

override_dh_auto_build:
	$(MAKE) dist manpages

override_dh_auto_test:
	@:

export PREFIX RMQ_ROOTDIR

override_dh_auto_install: PREFIX = /usr
override_dh_auto_install: RMQ_ROOTDIR = $(PREFIX)/lib/rabbitmq
override_dh_auto_install: RMQ_ERLAPP_DIR = $(RMQ_ROOTDIR)/lib/rabbitmq_server-*
override_dh_auto_install:
	dh_auto_install

	$(MAKE) install-bin DESTDIR=$(DEB_DESTDIR)

	sed \
		-e 's|@RABBITMQ_USER@|$(RABBITMQ_USER)|' -e 's|@RABBITMQ_GROUP@|$(RABBITMQ_GROUP)|' \
		< scripts/rabbitmq-script-wrapper \
		> $(DEB_DESTDIR)$(PREFIX)/sbin/rabbitmqctl
	chmod 0755 $(DEB_DESTDIR)$(PREFIX)/sbin/rabbitmqctl
	for script in rabbitmq-server rabbitmq-plugins rabbitmq-diagnostics rabbitmq-queues rabbitmq-upgrade; do \
		cp -a $(DEB_DESTDIR)$(PREFIX)/sbin/rabbitmqctl \
		 $(DEB_DESTDIR)$(PREFIX)/sbin/$$script; \
	done
	install -p -D -m 0644 debian/rabbitmq-server.default \
		$(DEB_DESTDIR)/etc/default/rabbitmq-server

	install -p -D -m 0755 scripts/rabbitmq-server.ocf \
		$(DEB_DESTDIR)$(PREFIX)/lib/ocf/resource.d/rabbitmq/rabbitmq-server
	install -p -D -m 0755 scripts/rabbitmq-server-ha.ocf \
		$(DEB_DESTDIR)$(PREFIX)/lib/ocf/resource.d/rabbitmq/rabbitmq-server-ha

	install -p -D -m 0755 scripts/rabbitmqctl-autocomplete.sh \
		$(DEB_DESTDIR)/etc/profile.d/rabbitmqctl-autocomplete.sh

	install -p -D -m 0755 scripts/zsh_autocomplete.sh \
		$(DEB_DESTDIR)/usr/share/zsh/vendor-functions/_enable_rabbitmqctl_completion

	rm $(DEB_DESTDIR)$(RMQ_ERLAPP_DIR)/LICENSE* \
		$(DEB_DESTDIR)$(RMQ_ERLAPP_DIR)/INSTALL

	rmdir $(DEB_DESTDIR)$(PREFIX)/lib/erlang/lib \
		$(DEB_DESTDIR)$(PREFIX)/lib/erlang
